import {
  getSetting,
  openSetting,
  chooseAddress,
  showModal,
  showToast
} from '../../utils/asyncWx.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address = wx.getStorageSync("address");
    const cart = wx.getStorageSync("cart") || [];
    //数组every方法：遍历数组，如果数据都为true则返回true，只要有一个是false则返回false
    this.setCart(cart);
    this.setData({
      address
    })
  },

 async handleChooseAddress() {
    //版本更新...不用授权，直接调用获取地址api接口
   let address = await chooseAddress();
   address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
      wx.setStorageSync("address", address);
  },
 
  handleItemChange(e) { 
    const goods_id = e.currentTarget.dataset.id;
    let { cart } = this. data;
    let index = cart.findIndex(v => v.goods_id === goods_id);
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },

  handleItemAllchange() {
    let { cart, allChecked } = this. data;
    allChecked = !allChecked;
    cart.forEach(v => v.checked = allChecked);
    this.setCart(cart);
  },

  async handleNumEdit(e) { 
    let { cart } = this.data;
    const { operation, id } = e.currentTarget.dataset;
    const index = cart.findIndex(v => v.goods_id === id);
    if (cart[index].num === 1 && operation === -1) {
      const res = await showModal({ content: "你确定要删除该商品吗？" });
      if (res.confirm) { 
        cart.splice(index, 1);
        this.setCart(cart);
      }
    } else { 
      cart[index].num += operation;
      this.setCart(cart);
    }
  },

 async handlePay() {
   const { address, totalNum } = this.data;
   if (!address.userName) { 
     await showToast({ title: "您还没有添加收获地址！" });
     return;
   }
   if (totalNum === 0) { 
     await showToast({ title: "您还没有添加商品！" });
     return;
   }
   wx.navigateTo({
     url: '/pages/pay/index',
   });
   },

  setCart(cart) { 
    let allChecked = true;
    let totalNum = 0;
    let totalPrice = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalNum += v.num;
        totalPrice += v.num * v.goods_price;
      } else { 
        allChecked = false;
      }
    });
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
      cart,
      allChecked,
      totalNum,
      totalPrice
    });
    wx.setStorageSync("cart", cart);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})